//
//  DataHander.m
//  CarFace
//
//  Created by dlios on 15-4-2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "DataHander.h"

@implementation DataHander
+ (DataHander *)shareInstance
{
    //  单例方法是实现
    static DataHander *dbHander = nil;
    //   当静态指针为空, 要创建一个对象
    if (dbHander == nil) {
        dbHander = [[DataHander alloc]init];
        //        不能释放,
    }
    return dbHander;
}


- (void)openDB
{// 打开数据库
    //拼接一个数据库路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [docPath stringByAppendingPathComponent:@"/zzmCanFace.db"];
    NSLog(@"dbPath    %@", dbPath);
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    NSLog(@"打开数据库结果:  %d", result);
    [self judgeResult:result type:@"打开数据库"];
}


- (void)createTable
{// 创建moveSave表
    NSString *sqlStr = @"create table forumSave (bbsname text primary key, bbsid text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"创建表"];
    
}



- (void)insertForumSave:(Listing *)forumSave
{// 添加数据
    NSString *sqlStr = [NSString stringWithFormat:@"insert into forumSave values('%@', '%@')", forumSave.bbsname, forumSave.bbsid];
//    NSLog(@"%@", sqlStr);
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"插入数据"];
    
}



- (NSMutableArray *)selectAll
{// 查询并获取数据
    NSString *sqlStr = @"select * from forumSave";
    //   申明数据库指针的状态指针, 数据库执行sql语句的所有结果都保存到这个指针中
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(dbPoint, [sqlStr UTF8String], -1, &stmt, NULL);
    NSMutableArray *forumSave = [NSMutableArray array];
    if (result == SQLITE_OK) {
        //        遍历结果中的所有数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            Listing *movie = [[Listing alloc]init];
            
            const unsigned char *titleChar = sqlite3_column_text(stmt, 0);
            NSString *title = [NSString stringWithUTF8String:(const char *)titleChar];
            movie.bbsname = title;
            
            const unsigned char *idChar = sqlite3_column_text(stmt, 1);
            NSString *image = [NSString stringWithUTF8String:(const char *)idChar];
            movie.bbsid = image;
   
            [forumSave addObject:movie];
            [movie release];
        }
    }
    //    同一时间只能有一个管理员,所以必须回收状态指针内存, 将SQL的改变结果保存到数据库
    sqlite3_finalize(stmt);
    return forumSave;
    
}



- (void)deleteData:(NSString *)bbsid;
{// 删除数据
    NSString *deleteStr = [NSString stringWithFormat:@"delete from forumSave where bbsid = '%@'", bbsid];
//    NSLog(@"%@", deleteStr);
    int result = sqlite3_exec(dbPoint, [deleteStr UTF8String], NULL, NULL, NULL );
    [self judgeResult:result type:@"删除数据"];
}


- (void)closeDB
{//关闭数据库
    int result = sqlite3_close(dbPoint);
    [self judgeResult:result type:@"关闭数据库"];
}



- (void)judgeResult:(int)result type:(NSString *)type
{
    //判断结果的方法
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", type);
    }else{
        NSLog(@"%@失败, 失败编号:%d", type, result);
    }
    
}

@end
